<?php

namespace App\Http\Controllers;

use App\Clubvideo;
use App\Http\Requests\AddClubvideoRequest;
use App\MakeTable\Table;
use App\Redict\Success;
use App\SoftImage\SoftImage;
use Illuminate\Http\Request;
use Gate;
use Auth;
class ClubvideoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $pagename = '视频管理';
        $modelurl = 'clubvideos';
        $tablecate = ['tablecate'=>'视频'];
        $headname = ['视频名称','封面图片','视频链接','俱乐部ID','操作'];
        $tablehead = ['title','cover_image','video_url','club_id','id'];
        $clubvideostable = new Table($tablehead,$page = 8);
        if (Gate::allows('club_Admin_club')) {
            $model = $clubvideostable->viewdata(Clubvideo::class);
            $models = $clubvideostable->viewpagedata(Clubvideo::class);
        }
        else{
            $model = $clubvideostable->viewdatawithuser(Clubvideo::class);
            $models = $clubvideostable->viewpagedatawithuser(Clubvideo::class);
        }
        return view('admin.home.tableshow',compact('tablecate','model','tablehead','models','headname','pagename','modelurl'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //新增视频
        $modelurl = 'clubvideos';
        $pagename = '新增视频';
        $formhead = ['title','cover_image','video_url'];
        $formname = ['视频名称','封面图片','视频链接'];
        $formhead = collect($formname)->zip($formhead)->toArray();
        return view('admin.home.clunvideoform',compact('pagename','tags','modelurl','formhead'));
        //表单，位置 resources/views/admin/home/awardform.blade
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AddClubvideoRequest $request)
    {
        //添加验证
        $data = array_merge(
            ['club_id'=>\Auth::user()->clubroleid(),],
            $request->all(),
            SoftImage::cover($request->file('cover_image'))//处理图片的函数
        );
        //dd($data);
        $train = Clubvideo::create($data);
//        $soft->tags()->attach($request->input('tags_list')); duoduiduo

        return redirect('admin/clubvideos')->with('message', '成功添加'.$train->name);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $train = Clubvideo::findOrFail($id);
        $modelurl = 'clubvideos';
        $pagename = $train->name;
        $formhead = ['title','cover_image','video_url'];
        $formname = ['视频名称','封面图片','视频链接'];
        $formhead = collect($formname)->zip($formhead)->toArray();
        //$mentors = \Auth::user()->clubmodelforrole()->mentors->pluck('name','id');
        //$mentor_id = $train->mentor->id;
        return view('admin.home.edittrain',compact('pagename','train','mentors','mentor_id','formhead','modelurl'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //添加验证
        $activity = Clubvideo::findOrFail($id);
        //查找
        $data = array_merge(
            $request->all(),
            ['mentor_id'=>$request->input('mentor')]
        );
        if($request->file('cover_image')){

            $data = array_merge($data, SoftImage::cover($request->file('cover_image')));
        }

        $activity->update($data);
        //更新
        return Success::back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        Clubvideo::destroy($id);
        return Success::back();
    }
}
